/*
 * Copyright (c) csii.com.cn 2016 zhaojin
 */

package com.csii.ssm.controller;
import com.csii.ssm.core.CommonDict;
import com.csii.ssm.dto.AjaxData;
import com.csii.ssm.exception.CoreRuntimeException;
import com.csii.ssm.model.type.RuleType;
import com.csii.ssm.util.ControllerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zhaojin on 15/6/1.
 */
public abstract class BaseController {

    Logger logger = LoggerFactory.getLogger(BaseController.class);

    public static final String REDIRECT = "redirect:";
    public static final String ERRMSG = String.valueOf(AjaxData.AjaxDataField.errmsg);
    public static final String ERRCODE = String.valueOf(AjaxData.AjaxDataField.errcode);

    //登录用户存储在session中的key
    public static final String USER = String.valueOf(RuleType.Type.USER);

    protected String prifxViewFolderPath;

    @Value("#{mvcContext['app.contextpath']}")
    protected String APPCONTEXTPATH;

    @Value("#{mvcContext['500.trans.code']}")
    protected String TRANSCODE_500;

    @ExceptionHandler(value = {CoreRuntimeException.class})
    public void handlerException(CoreRuntimeException e, HttpServletResponse resp, HttpServletRequest req) {
        innerHandlerException(e, resp, req, TRANSCODE_500);
    }

    protected void innerHandlerException(CoreRuntimeException e, HttpServletResponse resp, HttpServletRequest req, String transcode500) {
        ControllerUtil.innerHandlerException(e, resp, req, (APPCONTEXTPATH + transcode500));
    }


    protected AjaxData getAjaxData() {
        AjaxData ajaxData = new AjaxData();
        ajaxData.setStatus(CommonDict.AJAXDATA_STATUS_SUCCESS);
        ajaxData.setMsg(CommonDict.AJAXDATA_STATUS_SUCCESS_MSG);
        return ajaxData;
    }

    protected AjaxData getAjaxData(String status) {
        AjaxData ajaxData = new AjaxData();
        ajaxData.setStatus(status);
        return ajaxData;
    }
}
